package cn.itcast.action;

import java.util.List;
import java.util.Map;

import javax.enterprise.inject.New;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.util.ValueStack;

import cn.itcast.entity.Dept;
import cn.itcast.service.IDeptService;

public class DeptAction extends ActionSupport implements ModelDriven<Dept>{

	private Dept dept = new Dept();
	public void setDept(Dept dept) {
		this.dept = dept;
	}
	public Dept getDept() {
		return dept;
	}
	
	@Override
	public Dept getModel() {
		return dept;
	}
	
	private IDeptService deptService;
	public void setDeptService(IDeptService deptService) {
		this.deptService = deptService;
	}
	public IDeptService getDeptService() {
		return deptService;
	}
	
	public String viewAdd(){
		return "add";
	}
	
	public String add(){
		
		deptService.save(dept);
		
		return "listAction";
	}
	
	public String listDept(){
		List<Dept> listDept = deptService.getAll();
		
		Map<String, Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
		request.put("listDept", listDept);
		
		return "listDept";
	}

	public String viewUpdate(){
		
		int id = dept.getId();
		Dept dep = deptService.findById(id);
		
		ValueStack stack = ActionContext.getContext().getValueStack();
		stack.pop();
		stack.push(dep);
		
		return "viewUpdate";
	}
	
	public String update(){
		
		deptService.update(dept);
		
		return "listAction";
	}
	
	
}
